package com.ruoyi.web.controller.reseller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.user.IVsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.reseller.VsDramasResellerUser;
import com.ruoyi.system.service.reseller.IVsDramasResellerUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * This file is part of 南阳迈特网络科技有限公司.
 * @company  南阳迈特网络科技有限公司
 * @link     https://www.nymaite.com
 * @document https://product.nymaite.cn
 * @contact  service@nymaite.com
 * @author nymaite
 */
/**
 * 分销用户Controller
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Api(tags = "分销用户")
@RestController
@RequestMapping("/reseller/user")
public class VsDramasResellerUserController extends BaseController
{
    @Autowired
    private IVsDramasResellerUserService vsDramasResellerUserService;

    /**
     * 查询分销用户列表
     */
    @ApiOperation("查询分销用户列表")
    @GetMapping("/list")
    public AjaxResult list(VsDramasResellerUser vsDramasResellerUser)
    {
        VsUser user = SecurityUtils.getLoginUser().getVsUser();
        if (user == null) {
            return error("未获取到用户信息！");
        }
        if ("guest".equals(user.getMode())) {
            return error();
        }
        vsDramasResellerUser.setResellerUserId(Math.toIntExact(user.getId()));
        startPage();
        List<VsDramasResellerUser> list = vsDramasResellerUserService.selectVsDramasResellerUserList(vsDramasResellerUser);
        TableDataInfo dataTable = getDataTable(list);
        int directUser = 0;
        int indirectUser = 0;
        for (VsDramasResellerUser u : list) {
            if ("1".equals(u.getType())) {
                directUser++;
            } else {
                indirectUser++;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("list", dataTable.getRows());
        map.put("countUser", dataTable.getTotal());
        map.put("directUser", directUser);
        map.put("indirectUser", indirectUser);
        return success(map);
    }

    /**
     * 导出分销用户列表
     */
//    @ApiOperation("导出分销用户列表")
    @Log(title = "分销用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VsDramasResellerUser vsDramasResellerUser)
    {
        List<VsDramasResellerUser> list = vsDramasResellerUserService.selectVsDramasResellerUserList(vsDramasResellerUser);
        ExcelUtil<VsDramasResellerUser> util = new ExcelUtil<VsDramasResellerUser>(VsDramasResellerUser.class);
        util.exportExcel(response, list, "分销用户数据");
    }

    /**
     * 获取分销用户详细信息
     */
    @ApiOperation("获取分销用户详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(vsDramasResellerUserService.selectVsDramasResellerUserById(id));
    }

    /**
     * 新增分销用户
     */
    @ApiOperation("新增分销用户")
    @Log(title = "分销用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsDramasResellerUser vsDramasResellerUser)
    {
        return toAjax(vsDramasResellerUserService.insertVsDramasResellerUser(vsDramasResellerUser));
    }

    /**
     * 修改分销用户
     */
//    @ApiOperation("修改分销用户")
    @Log(title = "分销用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsDramasResellerUser vsDramasResellerUser)
    {
        return toAjax(vsDramasResellerUserService.updateVsDramasResellerUser(vsDramasResellerUser));
    }

    /**
     * 删除分销用户
     */
//    @ApiOperation("删除分销用户")
    @Log(title = "分销用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(vsDramasResellerUserService.deleteVsDramasResellerUserByIds(ids));
    }
}
